<template>
	<view class="urgent">
		<view class="info">
			<view class="info_left">
				紧急救助
			</view>
			<view class="info_right">
				<u-switch
					space="6" 
					v-model="urgentVal" 
					:activeValue="true"
					:inactiveValue="false"
					activeColor="#f9ae3d" 
					inactiveColor="rgb(230, 230, 230)"
					size="40"
					@change="changeVal">
				</u-switch>
			</view>
		</view>
		<view class="info" @click="openShow"> 
			<view class="info_left">
				添加紧急联系人
			</view>
			<view class="info_right">
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		
		<!-- 紧急联系人列表 -->
		<view class="list">
			<view class="list-item info" v-for="item in list" :key="item.id">
				<view class="info">
					<view class="info_left">{{item.name}}</view>
					<view class="info_right">{{item.phone}}</view>
				</view>
				<view @click="handleDel(item.id)">删除</view>
			</view>
		</view>
		
		<view class="info">
			<view class="info_left">
				添加 “120”
			</view>
			<view class="info_right">
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		
		<u-popup :show="show" mode="center"  @close="show=false" @open="show=true">
			<view class="show_container">
					<text class="title">添加紧急联系人</text>
					<view class="show_info">
						<!-- <input type="text" placeholder="请输入姓名" v-model="addParmas.name"> -->
						<!-- <input type="text" placeholder="请输入手机号" v-model="addParmas.phone"> -->
						<u--input
							placeholder="请输入姓名"
							border="surround"
							v-model="addParmas.name"
						></u--input>
						<u--input
							placeholder="请输入手机号"
							border="surround"
							v-model="addParmas.phone"
						></u--input>
					</view>
					<view class="btn" @click="addPhone">确认</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { getContactsList, addContacts, delContacts } from '@/service/api.js'
	import funUtils from '@/utils/fun.js'
	export default {
		data() {
			return {
				urgentVal: null,
				
				list: [],
				show: false,
				addParmas: {
					name: "",
					phone: ""
				}
			}
		},
		onLoad() {
			this.getList();
			this.urgentVal = Boolean(uni.getStorageSync("userInfo").contactsReminder);
		},
		methods: {
			changeVal(e) {
				funUtils.setUserInfoSuccss('contactsReminder', this.urgentVal)
			},
 			getList() {
				getContactsList(1, 10).then(res => {
					if(res.status == 200) this.list = res.data;
				})
			},
			handleDel(id) {
				const that = this;
				uni.showModal({
					title: "是否确认删除该联系人",
					success: function (e) {
						if (e.confirm) {
							delContacts({id}).then(res => {
								if(res.status == 200) {
									uni.showToast({
										duration: 1000,
										title: "删除成功",
										icon: 'success',
										success: () => {
											that.getList()
										}
									})
								}
							})
						}
					}
				})	
			},
			openShow() {
				if(this.list.length >= 2) {
					uni.showToast({
						duration: 1000,
						title: "紧急联系人数量不能大于2人",
						icon: 'none'
					})
				} else {
					this.show = true;
				}
			},
			addPhone() {
				addContacts(this.addParmas).then(res => {
					if(res.status == 200) {
						uni.showToast({
							duration: 1000,
							title: "添加成功",
							success: () => {
								this.show = false;
								this.getList();
								this.addParmas = {
									name: "",
									phone: ""
								}
							}
						})
					} else {
						uni.showToast({
							duration: 1000,
							title: res.message
						})
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
.urgent {
	width: 100%;
	height: 100vh;
	background-color: #eee;
	overflow: hidden;
	.info {
		margin: 30rpx 20rpx ;
		height: 120rpx;
		background-color: #fff;
		border-radius: 32rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 40rpx;
	}
	
	.list {
		// background-color: #fff;
		.list-item {
			// background-color: #eee;
			color: red;
			height: 80rpx;
			border-radius: 0;
		}
	}
	
	
	.show_container {
		padding: 20rpx 40rpx;
		text-align: center;
		.title {
			// padding-bottom: 1	0rpx;
			display: block;
		}
		.show_info {
			margin: 20rpx 0;
			::v-deep .u-input {
				margin: 20rpx 0;
			}
		}
		.btn {
			height: 80rpx;
			line-height: 80rpx;
			background-color: red;
			color: #fff;
			text-align: center;
			border-radius: $uni-border-radius-16;
		}
	}
}
</style>
